const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            Organization: [],
            Member: [],
            FeeTypeList: [
                { value: "正常", label: "正常" },
                { value: "补缴", label: "补缴" },
            ],
            FeetableData: [],
            FeetableDataCommit: [],
        };
    },
    mounted() {

        this.GetOrganization();

        this.dataForm.FeeType = "正常";
        this.dataForm.Fee_Date = getYMDDate(new Date());
        this.dataForm.Belonginterval = getYMDate(new Date());


    },

    methods: {
        HarryFee() {
            this.loading = true;
            if (this.FeetableData.length > 0) {

                const filterArray = this.FeetableData.filter(item => item.bFee == '0')

                if (filterArray.length > 0) {
                    const Party_Member_SerialNumbers = filterArray.map(item => item.Party_Member_SerialNumber).join("-");

                    if (Party_Member_SerialNumbers) {
                        //调用催缴接口  sendPartyFeeMessage

                        // let strUrl = mh + '/wx/mp/sendPartyFeeMessage?Data=' + Party_Member_SerialNumbers + '&Msg=' + this.dataForm.Belonginterval + '期间,党费未缴纳,请及时缴费!';

                        let strUrl = mh + "/wx/mp/sendPartyFeeMessage?Data=" + Party_Member_SerialNumbers + "&Msg=" + this.dataForm.Belonginterval + '期间,党费未缴纳,请及时缴费!'

                        $.ajax({
                            headers: requestHeaders('JSON'),
                            // url: mh + '/requestForward/requestForwardForPost?RequestURL=' + encodeURIComponent(strUrl),
                            // url: `${mh}/wx/mp/sendPartyFeeMessage`,
                            url: mh + '/requestForward/requestForward?RequestURL=' + encodeURIComponent(strUrl),
                            // url:strUrl,
                            method: "GET",
                            // data: {
                            //     Data: Party_Member_SerialNumbers,
                            //     Msg: this.dataForm.Belonginterval + '期间,党费未缴纳,请及时缴费!'

                            // },
                            success: (res) => {
                                console.log(res);
                                this.$message({
                                    message: '已发送消息催缴!',
                                    type: 'success'
                                })

                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest)
                                this.loading = false;
                            },

                        });

                    }
                } else {
                    this.$message({
                        message: '没有需要催缴的数据',
                        type: 'warning'
                    })
                }
            }
            else {
                this.$message({
                    message: '没有需要催缴的数据',
                    type: 'warning'
                })


            }
        },

        handleSubmit1() {
            if (this.FeetableData.length > 0) {
                this.loading = true;
                let JsonObject = {
                    Add: { party_fees: [] },
                    Modify: { party_fees: [] }
                };

                this.FeetableData.forEach(item => {
                    const searchStr = '_Original';
                    const matchingKeys = Object.keys(item).filter(key =>
                        key.includes(searchStr)
                    );

                    if (matchingKeys.length > 0) {
                        // 是Modify，将item添加到Modify的party_fees数组中
                        JsonObject.Modify.party_fees.push({ ...item });  // 使用push添加新项
                    } else {
                        // 是Add，将item添加到Add的party_fees数组中
                        JsonObject.Add.party_fees.push({ ...item });  // 使用push添加新项
                    }
                });

                console.log(JsonObject);  // 查看最终结果

                let Operation_Main = JSON.stringify(deepClone(JsonObject));
                JsonObject['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(JsonObject, (res) => {
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                    this.loading = false;
                })
            } else {
                this.$message({
                    message: '没有需要保存的数据',
                    type: 'warning'
                });
            }
        },

        SelectAll() {
            this.FeetableData.forEach(item => {
                item.bFee = "1"
            })

        },
        SelectNone() {
            this.FeetableData.forEach(item => {
                item.bFee = "0"
            })
        },
        handleSelect(row) {
            // this.FeetableData.forEach(item => {
            //     item.bFee = "0"
            // })
            // row.bFee = "1"

        },

        changeFee_Date(val) {
            let _this = this;
            _this.dataForm.Belonginterval = val.substring(0, 7);

        },

        changeOrganization(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: 'f_party_member_information',
                    Condition: '',
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent(" and Party_Organization_SerialNumber='" + val + "'"))),

                },
                success: (data) => {
                    console.log("页面table1", data);
                    this.FeetableData = data;
                    //查询原值
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        method: "POST",
                        data: {
                            Data: 'party_fees', //sql名
                            Condition: "Row_Status='0' and Party_Organization_SerialNumber='" + val + "' and Belonginterval='" + this.dataForm.Belonginterval + "'"
                        },
                        success: (res) => {
                            console.log("页面table2", res);
                            const mergedData = this.FeetableData.map(item => {
                                const matchedItems = res.filter(i =>
                                    i.Party_Member_SerialNumber === item.Party_Member_SerialNumber
                                );

                                // 假设 res 中每个 Party_Member_SerialNumber 是唯一的，取第一个匹配项
                                const matchedItem = matchedItems[0] || {};

                                return {
                                    ...item,
                                    ...matchedItem,
                                    Belonginterval: this.dataForm.Belonginterval,
                                    Fee_Date: this.dataForm.Fee_Date,

                                };
                            });

                            // 使用合并后的数据
                            this.FeetableData = mergedData;
                            console.log("页面table合并", this.FeetableData);

                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    })
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

        },
        GetOrganization() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/permissionsTree/getPartyOrganizationsTreeByPermissions`,
                type: "GET",
                data: {
                    PartyOrganizationsCode: getCookie('party'), //请求参数
                },
                success: (data) => {
                    const _this = this;
                    _this.Organization = _this.convertToCascaderFormat(data); // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

        },
        validateInput() {
            let _this = this;
            if (_this.dataForm.Fee_Amount < 0) {
                _this.dataForm.Fee_Amount = 0;
            } else if (_this.dataForm.Fee_Amount > 9999) {
                _this.dataForm.Fee_Amount = 9999;
            }

            //_this.dataForm.Fee_Amount=parseFloat(_this.dataForm.Fee_Amount.toFixed(2))
        },
        convertToCascaderFormat(items) {
            const _this = this;
            console.log(items);
            return items.map((item) => ({
                label: item.Party_Organization_Name,
                value: item.Party_Organization_SerialNumber,
                // 递归处理子节点
                children: item.children
                    ? _this.convertToCascaderFormat(item.children)
                    : [],
            }));
        },

    },

};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-select', ylSelect);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
